package com.service.impl;

import com.dao.TokenDao;
import com.domain.dto.MaintenanceManDTO;
import com.domain.entity.TokenEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utils.PageUtils;


import com.dao.WeixiuyuanDao;
import com.domain.entity.WeixiuyuanEntity;
import com.service.WeixiuyuanService;
import org.springframework.transaction.annotation.Transactional;

@Service("weixiuyuanService")
public class WeixiuyuanServiceImpl extends ServiceImpl<WeixiuyuanDao, WeixiuyuanEntity> implements WeixiuyuanService {

    @Autowired
    private WeixiuyuanDao weixiuyuanDao;

    @Autowired
    private TokenDao tokenDao;


    @Override
    public PageUtils queryPage(int pageNum, int pageSize, WeixiuyuanEntity weixiuyuanEntity) {

        EntityWrapper<WeixiuyuanEntity> wrapper = new EntityWrapper<>();

        if (StringUtils.isNotEmpty(weixiuyuanEntity.getJobNumber())){
            wrapper.like("job_number",weixiuyuanEntity.getJobNumber());
        }

        if (StringUtils.isNotEmpty(weixiuyuanEntity.getUsername())){
            wrapper.like("username",weixiuyuanEntity.getUsername());
        }
        Page<WeixiuyuanEntity> page = new Page<>(pageNum, pageSize);
        page.setRecords(weixiuyuanDao.selectPage(page, wrapper));

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateByJobNumber(MaintenanceManDTO maintenanceManDTO, String userid) {

        //除了要修改维修员表中的信息还要修改token表中的信息
        weixiuyuanDao.updateByJobNumber(maintenanceManDTO);
        TokenEntity tokenEntity = new TokenEntity();
        BeanUtils.copyProperties(maintenanceManDTO, tokenEntity);
        tokenEntity.setUserid(Long.valueOf(userid));
        tokenDao.update(tokenEntity, new EntityWrapper<TokenEntity>().eq("userid", Long.valueOf(userid)));

    }


}
